/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.flagmansworld;

import java.util.Objects;

/**
 * Application-Object with Name, Package-Name, if the App installed and the Source
 * @author Patrick Wachsmuth
 * @version 1.1
 */
public class AppObj {

    private String name;
    private String apkPackage;
    private boolean installed;
    private String source;

    /**
     * Creates a full AppObj
     * @param name
     * @param apkPackage
     * @param installed
     * @param source 
     * @exception IllegalArgumentException
     */
    public AppObj(String name, String apkPackage, boolean installed, String source) throws IllegalArgumentException {
        if (name == null || name.isEmpty() || apkPackage == null || apkPackage.isEmpty() || source == null || source.isEmpty()) {
            throw new IllegalArgumentException("Parameter are empty or null");
        } else {
            this.name = name;
            this.apkPackage = apkPackage;
            this.installed = installed;
            this.source = source;
        }
    }

    /**
     * Creates a empty AppObj
     */
    public AppObj() {
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     * @exception IllegalArgumentException
     */
    public void setName(String name) throws IllegalArgumentException {
        if (name == null || name.isEmpty()) {
            throw new IllegalArgumentException("Parameter are empty or null");
        } else {
            this.name = name;
        }
    }

    /**
     * @return the installed
     */
    public boolean isInstalled() {
        return installed;
    }

    /**
     * @param installed the installed to set
     */
    public void setInstalled(boolean installed) {
        this.installed = installed;
    }

    /**
     * @return the source
     */
    public String getSource() {
        return source;
    }

    /**
     * @param source the source to set
     * @exception IllegalArgumentException
     */
    public void setSource(String source) throws IllegalArgumentException {
        if (source == null || source.isEmpty()) {
            throw new IllegalArgumentException("Parameter are empty or null");
        } else {
            this.source = source;
        }
    }

    /**
     * @return the apkPackage
     */
    public String getApkPackage() {
        return apkPackage;
    }

    /**
     * @param apkPackage the apkPackage to set
     * @exception IllegalArgumentException
     */
    public void setApkPackage(String apkPackage) throws IllegalArgumentException {
        if (apkPackage == null || apkPackage.isEmpty()) {
            throw new IllegalArgumentException("Parameter are empty or null");
        } else {
            this.apkPackage = apkPackage;
        }
    }

    /**
     * Returns the Object as String
     * @return Object as String
     */
    @Override
    public String toString() {
        return "name: " + this.getName() + " apkPackage: " + this.getApkPackage() + " isInstalled: " + this.isInstalled() + " source: " + this.getSource();
    }

    /**
     * Merge two Objects
     * @param obj
     * @return true if the Objects are identical. Otherwise false
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (this.getClass() == obj.getClass()) {
            AppObj a = (AppObj) obj;
            if (this.hashCode() == a.hashCode()) {
                if (this.isEqualTo(a)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }

        return false;
    }

    /**
     * Generates the HashCode from the actual Object
     * @return the hash
     */
    public int hashCode() {
        int hash = 7;
        hash = 71 * hash + Objects.hashCode(this.name);
        hash = 71 * hash + Objects.hashCode(this.apkPackage);
        hash = 71 * hash + (this.installed ? 1 : 0);
        hash = 71 * hash + Objects.hashCode(this.source);
        return hash;
    }

    /**
     * Method to Compare the Objects with the Values for each Attribute
     * @param obj
     * @return true if the Object-Params are identical. Otherwise false
     */
    private boolean isEqualTo(AppObj obj) {
        if (!this.name.equals(obj.name)) {
            return false;
        }
        if (!this.apkPackage.equals(obj.apkPackage)) {
            return false;
        }
        if (!this.source.equals(obj.source)) {
            return false;
        }
        if (!this.installed == obj.installed) {
            return false;
        }

        return true;
    }
}
